import sys
from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtCore import QPropertyAnimation
from com.atzl.common.Win import Windows
from com.atzl.functions.extra.Scalp import Scalp
from com.atzl.common.ActionStates import ActionStates
from com.atzl.functions.accounting.func.Logs import CheckLogs
from com.atzl.ui.ScalpUI import Ui_MainWindow as scalp_ui


class ScalpWindow(QMainWindow, CheckLogs):

    def __init__(self):
        QMainWindow.__init__(self)
        # 设置为全局小部件
        self.ui = scalp_ui()
        self.scalp = Scalp()
        self.ui.setupUi(self)
        self.m_flag = False
        self.work = work_thread()
        global widgets
        widgets = self.ui
        global toscalp
        toscalp = self.scalp

        widgets.pushButton_2.clicked.connect(self.close)
        widgets.pushButton_3.clicked.connect(self.showMinimized)
        widgets.pushButton_6.clicked.connect(widgets.textEdit_2.clear)
        widgets.pushButton_5.clicked.connect(widgets.textEdit.clear)
        widgets.pushButton_7.clicked.connect(self.open_the_files)
        widgets.pushButton_8.clicked.connect(self.rollback_navigation)
        widgets.pushButton_4.clicked.connect(self.work.start)
        widgets.pushButton.clicked.connect(self.innit_odd_bale)
        self.work.work_thread_end.connect(self.thread_end)
        self.work.work_thread_starterro.connect(self.thread_starterro)
        self.work.work_thread_start.connect(self.thread_start)


    def innit_odd_bale(self):
        toscalp.create_odd_bale_template()


    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            self.m_flag = True
            self.m_Position = event.globalPos() - self.pos()  # 获取鼠标相对窗口的位置
            event.accept()
            self.setCursor(QCursor(QtCore.Qt.OpenHandCursor))  # 更改鼠标图标


    def mouseMoveEvent(self, QMouseEvent):
        if QtCore.Qt.LeftButton and self.m_flag:
            self.move(QMouseEvent.globalPos() - self.m_Position)  # 更改窗口位置
            QMouseEvent.accept()

    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag = False
        self.setCursor(QCursor(QtCore.Qt.ArrowCursor))

    def open_the_files(self):
        if toscalp.open_filed() is False:
            QMessageBox.information(self, '提示', '找不到目录!\n请重试',
                                    QMessageBox.Yes, QMessageBox.Yes)

    def thread_starterro(self):
        # 弹出一个消息对话框，参数为：self, 对话框名字， 对话框内容， 对话框按钮
        QMessageBox.information(self, "消息", "分配错误!",
                                QMessageBox.Ok)

    def thread_end(self):
        # 弹出一个消息对话框，参数为：self, 对话框名字， 对话框内容， 对话框按钮
        QMessageBox.information(self, "消息", "分配完成。",
                                QMessageBox.Ok)

    def thread_start(self):
        # 弹出一个消息对话框，参数为：self, 对话框名字， 对话框内容， 对话框按钮
        msgBox = QMessageBox()
        # msgBox.information(self, "消息", "开始分配..", QMessageBox.Ok)
        msgBox.setWindowTitle('提示')
        msgBox.setText("开始分配..")
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.button(QMessageBox.Ok).animateClick(1000)
        msgBox.exec()


    def show_anim(self):
        self.anim0 = QPropertyAnimation(self,
                                        b"windowOpacity")  # 设置动画对象
        self.anim0.setDuration(150)  # 设置动画时长
        self.anim0.setStartValue(0)  # 设置初始属性，1.0为不透明
        self.anim0.setEndValue(1)  # 设置结束属性，0为完全透明
        # self.anim.finished.connect(self.close)  # 动画结束时，关闭窗口
        self.anim0.start()  # 开始动画

    def rollback_navigation(self):
        self.hide()
        Windows.NAVIGATION_WIN.show()
        Windows.NAVIGATION_WIN.show_anim()
        self.logs_in(ActionStates.ROLLBACKNAVIGATION)

class work_thread(QThread):
    work_thread_starterro = pyqtSignal()
    work_thread_end = pyqtSignal()
    work_thread_start = pyqtSignal()

    def __init__(self):
        super().__init__()

    def analysis_path_odd(self):
        return widgets.textEdit.toPlainText().replace('file:///', '')

    def analysis_path_bale(self):
        return widgets.textEdit_2.toPlainText().replace('file:///', '')

    def operation_scalp_ui(self):
        return toscalp.operation_scalp(self.analysis_path_bale(),
                                       self.analysis_path_odd())

    def run(self):
        self.work_thread_start.emit()
        if self.operation_scalp_ui() is True:
            self.work_thread_end.emit()
        else:
            self.work_thread_starterro.emit()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = ScalpWindow()
    window.show()
    sys.exit(app.exec_())